%This function returns weighted averages of all values from the different
%blockmatrices

function [EstVal,EstLimits]=GetBlockEstimates(CovBlocks, Combinations, numOfStocks);

EstVal=zeros(numOfStocks, numOfStocks);
numOfBlocks=length(Combinations);
Count=zeros(numOfStocks, numOfStocks);

for iter=1:numOfBlocks
   combIter=Combinations{iter};
   lenIter=length(combIter);
   EstVal(combIter, combIter) = EstVal(combIter, combIter) + CovBlocks{iter};
   Count(combIter, combIter) = Count(combIter, combIter) + ones(lenIter, lenIter);
end

Count(find(Count==0))=1;
EstVal=EstVal./Count;

%Form a matrix with Sij=sqrt(EstVal(i,i)*EstVal(j,j))
EstLimits=sqrt(diag(diag(EstVal))*ones(size(EstVal))*diag(diag(EstVal)));